package com.yzt.plugin_widget.viewPager;

import android.os.Parcelable;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;

import java.util.List;

/**
 * author : bob
 * date   : 2022/11/29
 * desc   :
 */
public class ViewPagerAdapter extends FragmentStatePagerAdapter {

    private List<Fragment> mFragments;

    private String[] mTitles;

    public ViewPagerAdapter(FragmentManager fm, List<Fragment> mFragments, List<String> list) {
        super(fm);
        this.mFragments = mFragments;
        if (list != null) {
            mTitles = new String[list.size()];
            mTitles = list.toArray(mTitles);
        } else {
            mTitles = null;
        }
    }

    public void initData(List<Fragment> mFragments, String[] mTitles) {
        this.mFragments = mFragments;
        this.mTitles = mTitles;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mFragments.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        if (mTitles == null || position >= mTitles.length) {
            return "";
        }
        return mTitles[position];
    }

    @Override
    public Fragment getItem(int position) {
        return mFragments.get(position);
    }

    @Nullable
    @Override
    public Parcelable saveState() {
        return null;
    }

    @Override
    public void restoreState(Parcelable state, ClassLoader loader) {

    }
}